package com.ample16.stackdemo.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.context.ContextUtil;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.alibaba.csp.sentinel.slots.block.flow.FlowException;
import com.ample16.stackdemo.service.DemoService;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.io.FileWriter;
import java.util.concurrent.DelayQueue;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

@RestController
@RequestMapping("/hello")
public class TestController {
    @Autowired
    private DemoService demoService;

    @GetMapping("/kaka")
    public String test(@RequestParam("name") String name) {
        ContextUtil.enter("DemoService#bonjour","{node1}");
        return demoService.bonjour(name);
    }
    @Test
    public void test1(){
        Object o = new Object();
        synchronized (o) {
            try {
                o.wait(10000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("123");
        }
        ThreadLocal<String> stringThreadLocal = new ThreadLocal<>();
        InheritableThreadLocal<String> inheritableThreadLocal = new InheritableThreadLocal<>();
        stringThreadLocal.set("parent");
        inheritableThreadLocal.set("inheritableThreaLocal");
        ExecutorService executorService = Executors.newFixedThreadPool(10);
        for (int i = 0; i < 10; i++) {
            int finalI = i;
            executorService.submit(new Runnable() {
                @Override
                public void run() {
//                    stringThreadLocal.set("hello"+ finalI);
                    System.out.println(stringThreadLocal.get());
                    System.out.println(inheritableThreadLocal.get());
                }
            });
        }
        System.out.println(stringThreadLocal.get());

        stringThreadLocal.remove();
    }

    public static void main(String[] args) {
        Object o1 = new Object();
        Object o2 = new Object();
        Thread thread1 = new Thread(() -> {
            synchronized (o1) {
                System.out.println(Thread.currentThread().getName() + ":o1");
                try {
                    Thread.sleep(10);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                synchronized (o2) {
                    System.out.println(Thread.currentThread().getName() + ":o2");
                }
            }
        });
        Thread thread2 = new Thread(() -> {
            synchronized (o2) {
                System.out.println(Thread.currentThread().getName() + ":o2");
                synchronized (o1) {
                    System.out.println(Thread.currentThread().getName() + ":o1");
                }
            }
        });
       thread1.start();
       thread2.start();
        try {
            thread1.join();
            thread2.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("finish====");
    }
}
